package com.bstek.bdf4.mq.admin.impl;

import com.bstek.bdf2.core.mq.publisher.BstekRabbitAdmin;
import com.bstek.bdf2.core.mq.publisher.RabbitQueueInfo;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueInformation;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Title: BstekRabbitAdminImpl
 * @Package: com.bstek.bdf4.mq.admin.impl
 * @Description:
 * @Author: zxq
 * @Date: 2024/2/6 - 21:44
 */
@Service
public class BstekRabbitAdminImpl implements BstekRabbitAdmin {

    @Resource
    private  RabbitAdmin rabbitAdmin;

    @Override
    public void declareExchange(String exchangeName) {
        rabbitAdmin.declareExchange(new DirectExchange(exchangeName));
    }

    @Override
    public void declareQueue(String queueName) {
        rabbitAdmin.declareQueue(new Queue(queueName));

    }

    @Override
    public void declareBinding(String queueName, String exchangeName, String routingKey) {
        rabbitAdmin.declareBinding(new Binding(queueName, Binding.DestinationType.QUEUE, exchangeName, routingKey, null));

    }

    @Override
    public void deleteExchange(String exchangeName) {
        rabbitAdmin.deleteExchange(exchangeName);
    }

    @Override
    public void deleteQueue(String queueName) {
        rabbitAdmin.deleteQueue(queueName);
    }

    @Override
    public void purgeQueue(String queueName) {
        rabbitAdmin.purgeQueue(queueName, false);
    }

    @Override
    public RabbitQueueInfo getQueueInfo(String queueName) {

        QueueInformation queueInfo = rabbitAdmin.getQueueInfo(queueName);
        if(queueInfo==null){
            return null;
        }
        RabbitQueueInfo info = new RabbitQueueInfo();
        String name = queueInfo.getName();
        info.setName(queueInfo.getName());
        info.setConsumerCount(queueInfo.getConsumerCount());
        info.setMessageCount(queueInfo.getMessageCount());
        return info;
    }


}
